﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VitCode.ObjectResolutionHelper;

namespace VitCode.Castle.Entities
{
    [OrMemo("菜单")]
    public class Menu : BaseEntity
    {

        protected string m_Name;
        [OrMemo("名称")]
        public virtual string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        private string m_Operate;

        [OrMemo("操作描述")]
        public virtual string Operate
        {
            get { return m_Operate; }
            set { m_Operate = value; }
        }

        protected string m_UrlPath;
        [OrMemo("Url路径")]
        public virtual string UrlPath
        {
            get { return m_UrlPath; }
            set { m_UrlPath = value; }
        }

        protected string m_SortKey;
        [OrMemo("排序关键字")]
        public virtual string SortKey
        {
            get { return m_SortKey; }
            set { m_SortKey = value; }
        }

        protected Menu m_Parent;
        [OrMemo("父菜单")]
        public virtual Menu Parent
        {
            get { return m_Parent; }
            set
            {
                if(value!=null&&value==this)
                    throw new ArgumentException("不是設置本節點為父節點");
                m_Parent = value;
            }
        }

        protected string m_OrderId="";
        [OrMemo("OrderId")]
        public virtual string OrderId
        {
            get { return m_OrderId; }
            set
            {
                m_OrderId = m_Id.ToString();
                if(m_OrderId==null || m_OrderId.Length < m_Id.ToString().Length)
                    throw new ArgumentException("OrderId節點資訊有誤");
                if (m_Parent != null)
                    m_OrderId = m_Parent.OrderId + "." + m_OrderId;
                m_OrderId = value;
            }
        }

        /// <summary>
        /// 重寫Equals方法
        /// </summary>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(obj, this))
                return true;
            if (obj == null)
                return false;
            var sObj = obj as Menu;
            return sObj != null && (sObj.Id == m_Id || sObj.Name == m_Name);
        }

        /// <summary>
        /// 重寫GetHashCode方法
        /// </summary>
        public override int GetHashCode()
        {
            int hash = 22;
            hash = 27 * hash * m_Id.GetHashCode();
            return hash;
        }
    }
}
